package com.joelemon.mockinfluxdbdata.mock.boss;

import com.joelemon.mockinfluxdbdata.common.SystemException;
import com.joelemon.mockinfluxdbdata.mock.wokers.DBInsertWokerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description: 控制服务
 * @Author: HJY
 * @Date: 2024/1/5
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ControllService {

    private final Map<String, DBInsertWokerService> serviceMap;

    private BossRunner bossRunner;

    private DBInsertWokerService getService(String serviceName) {
        DBInsertWokerService dbInsertWokerService = serviceMap.get(serviceName);
        if (dbInsertWokerService == null) {
            throw new SystemException("启动服务未找到");
        }
        return dbInsertWokerService;
    }

    public void start(String serviceName) {
        synchronized (ControllService.class) {
            if (bossRunner == null) {
                bossRunner = new BossRunner(getService(serviceName));
                bossRunner.start();
            } else {
                throw new SystemException("服务已启动");
            }
        }
    }
    public void stop() {
        synchronized (ControllService.class) {
            if (bossRunner != null) {
                bossRunner.stop();
            } else {
                throw new SystemException("服务尚未启动");
            }
        }
    }
}
